package com.linkage.jz.bankruptcy.utils;

import android.text.TextUtils;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 签名工具类
 */
public class SignUtil {

    /*
     *创建sign
     * appkey和params都是计算md5的参数，先进行排序，然后进行计算
     */
    public static String createSign(String appkey, Map<String, String> params) {
//        Map<String, Object> map = Map.sortMapByKey(params);
        Map<String, String> map =params;
        StringBuffer sb = new StringBuffer();
        Set<String> keySet = map.keySet();
        Iterator<String> it = keySet.iterator();
//        while (it.hasNext()) {
//            String k = it.next();
//            String v = (String) map.get(k);
//            if (null != v && !"".equals(v)
//                    && !"sign".equals(k)) {
//                sb.append(k + "=" + v + "&");
//            }
//        }
        String v = (String) map.get("appid");
        sb.append("appid" + "=" + v + "&");
        v = (String) map.get("nonce");
        sb.append("nonce" + "=" + v + "&");
        v = (String) map.get("timestamp");
        sb.append("timestamp" + "=" + v + "&");
        sb.append("appkey=" + appkey);

        String sign = md5(sb.toString());
//        String sign = MD5_tow(sb.toString());
//        String sign = md5("appid=1000000001&timestamp=1553248729545&nonce=6061963876832948701&appkey=1000000001");
        return sign;
    }

    public final static String MD5_tow(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    /*
     * md5计算
     */
    private static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
